import axios from './axios'
import {toast} from './toast'
import router from '../router'
import {removeDb, getDb} from './common'
const request = {
    // get方式
    async get (url, params) {
        // url =  url
        let { data: {code}, data } = await axios.get(url, { params: params })
        return new Promise((resolve) => {
            
            if (code == 10011) {
                toast('登录过期')
                setTimeout(() => {
                    removeDb('token')
                    router.push('/login')
                }, 1000)
            } else {
                resolve(data)
            }
        })
    },
    // post方式
    async post (url, params, flag = true) {
        let obj = {
            // transformRequest: [params => {
            //   let result = ''
            //   Object.keys(params).forEach((key) => {
            //     if (!Object.is(params[key], undefined) && !Object.is(params[key], null)) {
            //       result += encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) + '&'
            //     }
            //   })
            //   return result
            // }],
            headers: {
              'Content-Type': flag ? 'application/json' : 'application/x-www-form-urlencoded'
            }
        }
        // url = 'http://api.yaque365.com' + url
        let { data: {code}, data } = await axios.post(url, params, obj)
        return new Promise((resolve) => {
            if (code == 10011) {
                toast('登录过期')
                setTimeout(() => {
                    removeDb('token')
                    router.push('/login')
                }, 1000)
            } else {
                resolve(data)
            }
        })
    },
    async upLoad (url, params) {
        let obj = {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        }
        let {data: {code}, data} = await axios.post(url, params, obj) 
        return new Promise((resolve) => {
            if (code == 10011) {
                toast('登录过期')
                setTimeout(() => {
                    removeDb('token')
                    router.push('/login')
                }, 1000)
            } else {
                resolve(data)
            }
        })
    },
    // 表格文件下载
    async download (url, params) {
        let { data } = await axios({
            method: 'get',
            url,
            params: params,
            responseType: 'blob',
            headers: {
                'Content-Type': 'application/octet-stream',
                'X-Requested-With': 'XMLHttpRequest'
            }
        })
        return new Promise((resolve) => {
            // if (success) {
                // let fileName = data.headers['content-disposition'].split('filename=')[1] || '考勤统计'
                let fileName = '考勤统计.xlsx'
                fileName = decodeURI(fileName)
                // params.success(data.data)
                let blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
                if ('download' in document.createElement('a')) {
                    const elink = document.createElement('a')
                    const href = URL.createObjectURL(blob)
                    elink.download = fileName
                    elink.style.display = 'none'
                    elink.href = href
                    document.body.appendChild(elink)
                    elink.click()
                    URL.revokeObjectURL(href)
                    document.body.removeChild(elink)
                } else { // IE10+下载
                    navigator.msSaveBlob(blob, fileName)
                }
                resolve()
            // } else {
            //   // Toast.fail(message)
            //     reject(data)
            // }
        })
    }
}
export default request
